package com.lgcns.android2;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ProfilePopupActivity extends Activity implements OnClickListener{
	
	private String mTel;
	private String mEmail;
	
	@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.profile_popup);
        Intent intent = getIntent();
        
        //Get View from layout
        ImageView photo = (ImageView)findViewById(R.id.imgProfile);
        TextView name = (TextView)findViewById(R.id.name);
        TextView tel = (TextView)findViewById(R.id.tel);
        TextView email = (TextView)findViewById(R.id.email);
        TextView location = (TextView)findViewById(R.id.location);
        TextView job = (TextView)findViewById(R.id.job);

        //Setting value from intent
        mTel = intent.getStringExtra(Constants.TEL);
        mEmail = intent.getStringExtra(Constants.EMAIL);
        int idNum = intent.getIntExtra(Constants.ID_NUM, 0);

        Bitmap bm = Util.getProfileImage(this, idNum);
        
		photo.setImageBitmap(getRoundedCornerBitmap(bm, 16));
		name.setText(new StringBuffer(intent.getStringExtra(Constants.NAME))
				.append(" ").append(intent.getStringExtra(Constants.TITLE)));
		tel.setText(mTel);
		email.setText(mEmail);
		location.setText(intent.getStringExtra(Constants.LOCATION));
		job.setText(intent.getStringExtra(Constants.JOB));
        
		//Event Control
        Button btnCall = (Button)findViewById(R.id.btnCall);
        Button btnSms = (Button)findViewById(R.id.btnSms);
        Button btnClose = (Button)findViewById(R.id.btnClose);
        btnCall.setOnClickListener(this);
        btnSms.setOnClickListener(this);
        btnClose.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnClose:
			finish();
			break;
		case R.id.btnCall:
			startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mTel)));
			break;
		case R.id.btnSms:
			startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + mTel)));
			break;
		default:
			break;
		}
	}
	
	public Bitmap getRoundedCornerBitmap(Bitmap bitmap, int rt) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() , Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final int color = 0xff424242;
		final Paint paint= new Paint();
		final Rect rect = new Rect(0,0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = rt;
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		
		return output;
	}
}
